package com.ck.server.controller.test;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ck.common.constant.AjaxResult;
import com.ck.common.entity.SysLogininfor;
import com.ck.server.controller.BaseController;
import com.ck.system.service.SysLogininforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author ck
 * @date 2024/1/18 17:00
 */
@RestController
@RequestMapping("/monitor")
@Api(tags = "分页查询测试接口")
public class MonitorLoginInfoController extends BaseController {

    @Autowired
    private SysLogininforService sysLogininforService;
    /**
     *  基于MP的分页插件实现分页查询
     *  计算方式:startIndex=(pageNum-1) * pageSize
     */
    @GetMapping("/loginInfo")
    @ApiOperation(value = "基于MP的分页插件实现分页查询")
    public AjaxResult getLoginInfo(@RequestParam("pageNum") Integer page, @RequestParam("pageSize") Integer size){
        Integer pageNum = page==null ? 1: page;
        Integer pageSize = size==null ? 10: size;

        HashMap<String, Object> map = new HashMap<>();
        IPage<SysLogininfor> loginInfo = sysLogininforService.getLoginInfoForMP(pageNum, pageSize);
        map.put("data",loginInfo.getRecords());
        map.put("total",loginInfo.getTotal());
        return AjaxResult.success(map);
    }

    /**
     * 基于pagehelper 分页插件实现分页查询
     * startPage方法内部进行了封装,通过request请求去获取参数
     * 然后在通过PageHelper.startPage启动分页
     *
     * 需要注意的是,如果使用pagehelper分页插件,只会分页最近的一次查询
     */
    @GetMapping("/loginInfo2")
    @ApiOperation(value = "基于基于pagehelper的分页插件实现分页查询")
    public AjaxResult getLoginInfo2(){
        startPage();
        List<SysLogininfor> list = sysLogininforService.getLoginInfoForPH();
        return AjaxResult.success(getDataTable(list));
    }
}
